package main

import (
	"errors"

	"gitee.com/wu-jin-feng/kinfu/fgin"
	"gitee.com/wu-jin-feng/kinfu/fgin/demos/fmysql_test/dao"
	"github.com/gin-gonic/gin"
)

type User struct {
	Id int `form:"id" json:"id" binding:"required" msg:"id必填"`
}

func main() {

	f := fgin.Fgin{}
	r := f.GetHttpServer("./test.yaml")
	// 获得db
	dao.DB = f.GetGormDb("log_manager_db")
	r.GET("/get_user", func(ctx *gin.Context) {
		req := &User{}
		if err := fgin.FbindParams(ctx, req); err != nil {
			fgin.ReturnJson(ctx, 400, "", err)
			return
		}
		userDb := &dao.UserDao{}
		out, err := userDb.FindById(req.Id)
		if err != nil {
			fgin.ReturnJson(ctx, 400, "", err)
			return
		}
		if out.Id == 0 {
			fgin.ReturnJson(ctx, 400, "", errors.New("没有该用户"))
			return
		}
		fgin.ReturnJson(ctx, 200, out, nil)
	})
	r.POST("/get_user", func(ctx *gin.Context) {
		req := &User{}
		if err := fgin.FbindParams(ctx, req); err != nil {
			fgin.ReturnJson(ctx, 400, "", err)
			return
		}
		userDb := &dao.UserDao{}
		out, err := userDb.FindById(req.Id)
		if err != nil {
			fgin.ReturnJson(ctx, 400, "", err)
			return
		}
		if out.Id == 0 {
			fgin.ReturnJson(ctx, 400, "", errors.New("没有该用户"))
			return
		}
		fgin.ReturnJson(ctx, 200, out, nil)
	})
	f.Run(r)
}
